package two_ReverseInteger;

public class CSolution {

    public static void main(String[] args) {
        CSolution cSolution = new CSolution();
        System.out.println(cSolution.reverse(
            1534236469));
        System.out.println(cSolution.reverse(
            1463847412));
    }

    public int reverse(int x) {
        int flag = 1;
        if (x < 0) {
            flag = -1;
        }
        int result = 0;
        x = Math.abs(x);
        while (Math.ceil(x / 10) != 0) {
            result = result + x % 10;
            if (result < 0 || result > Integer.MAX_VALUE / 10) {
                return 0;
            }
            result = result * 10;
            if (result < 0) {
                return 0;
            }
            x = (int) Math.ceil(x / 10);
        }
        result = result + x;
        if (result < 0) {
            return 0;
        }
        return result * flag;
    }
}
